const express = require("express");
const qs = require("querystring");

const app = express();

// 定义:全局的中间件
app.use((req, res, next) => {
  let str = "";
  req.on("data", (chunk) => {
    str += chunk;
  });
  req.on("end", () => {
    req.body = qs.parse(str);
    console.log("自定义中间件", req.body);
    next();
  });
});

app.post("/login", (req, res) => {
  console.log(req.body);
  console.log(typeof req.body);
  const { username, password } = req.body;
  if (username === "admin" && password === "123456") {
    res.send("Login successful");
  } else {
    res.send("Login failed");
  }
});

app.listen(3000, () => {
  console.log("Server is running at http://127.0.0.1:3000");
});
